using System;
using System.Collections.Generic;
using System.Text;

namespace Acms.AcmsControls
{
    public abstract class BaseTemplateControl : System.Web.UI.UserControl
    {
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
        }

        private int currentPostId;
        public int CurrentPostId
        {
            get 
            {
                if (currentPostId == 0)
                    OnCurrentPostRequired();
                return currentPostId; 
            }
        }

        private int currentSiteId;
        public int CurrentSiteId
        {
            get 
            {
                if (currentSiteId == 0)
                    OnCurrentPostRequired();
                return currentSiteId;
            }
        }

        private void OnCurrentPostRequired()
        {
            if (CurrentPostRequired != null)
            {
                CurrentPostRequired(this, EventArgs.Empty);
            }
        }

        private Bll.Post currentPost;
        public Bll.Post CurrentPost
        {
            get
            {
                if (currentPost == null)
                {
                    OnCurrentPostRequired();
                }
                return currentPost;
            }
            set
            {
                if (value != null)
                {
                    this.currentPost = value;
                    this.currentPostId = value.Id;
                    this.currentSiteId = value.SiteId;
                }
            }
        }

        public event EventHandler CurrentPostRequired;

        protected override object SaveViewState()
        {
            ViewState["CurrentPostId"] = currentPostId;
            ViewState["CurrentSiteId"] = currentSiteId;
            return base.SaveViewState();
        }

        protected override void LoadViewState(object savedState)
        {
            base.LoadViewState(savedState);
            if (ViewState["CurrentPostId"] is int)
                currentPostId = (int)ViewState["CurrentPostId"];
            if (ViewState["CurrentSiteId"] is int)
                currentSiteId = (int)ViewState["CurrentSiteId"];
        }

        public Acms.Bll.PostControl FindPostControl(string name)
        {
            if (CurrentPost != null)
            {
                return CurrentPost.FindPostControl(name);
            }
            else
            {
                return null;
            }
        }

        public string HighlightText
        {
            get { return ViewState["HighlightText"] as string; }
            set { ViewState["HighlightText"] = value; }
        }

        protected string Highlight(string text)
        {
            if (text != null && HighlightText != null)
            {
                // Setup the regular expression and add the Or operator.
                System.Text.RegularExpressions.Regex regExp = new System.Text.RegularExpressions.Regex(string.Concat("\\b", HighlightText.Replace(" ", "|").Trim()), System.Text.RegularExpressions.RegexOptions.IgnoreCase);
                // Highlight keywords by calling the delegate each time a keyword is found.
                return regExp.Replace(text, new System.Text.RegularExpressions.MatchEvaluator(ReplaceKeyWords));
            }
            else
            {
                return text;
            }
        }

        private string ReplaceKeyWords(System.Text.RegularExpressions.Match m)
        {

            return "<span class=\"searchHighlight\">" + m.Value + "</span>";

        }

    }
}
